import { _decorator, Camera, CapsuleCollider, Collider, Color, Component, mat4, Mat4, Mesh, Node, Quat, SphereCollider, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('debugRenderer')
export class debugRenderer extends Component {

    @property(Camera)
    mainCamera: Camera = null;

    collider: Collider = null;

    capsuleCollider: CapsuleCollider = null;

    sphereCollider: SphereCollider = null;

    radius: number = 0;
    height: number = 0;

    start() {
        this.collider = this.node.getComponent(Collider);
        var worldBounds = this.collider.worldBounds;
        var boundSize = worldBounds.halfExtents;
        this.radius = boundSize.x;
        this.height = boundSize.y;
    }

    update(deltaTime: number) {
        var worldBounds = this.collider.worldBounds;
        var center = worldBounds.center;

        var boundSize = worldBounds.halfExtents;
        let worldRotation = this.collider.node.worldRotation;
        let worldPosition = this.collider.node.worldPosition;

        let reslut = new Mat4();
        let transform = new Mat4();
        Mat4.fromTranslation(reslut, worldPosition);
        Mat4.fromQuat(transform, worldRotation);
        reslut.multiply(transform);
        Mat4.fromTranslation(transform, new Vec3(-worldPosition.x, -worldPosition.y, -worldPosition.z));
        reslut.multiply(transform);
        if (this.mainCamera && this.mainCamera.camera.geometryRenderer) {
            if (this.collider instanceof CapsuleCollider) {
                this.mainCamera.camera.geometryRenderer.addCapsule(center, this.radius, this.height, Color.RED, 32, 8, false, true, false, true, reslut);
            } else if (this.collider instanceof SphereCollider) {
                this.mainCamera.camera.geometryRenderer.addSphere(center, this.radius, Color.RED, 32, 8, false, true, false, true, reslut);
            }
        }
    }
}

